package cn.iocoder.yudao.module.famous.doctor.service.researchgroup;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroup.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.researchgroup.ResearchgroupDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.researchgroup.ResearchgroupConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.researchgroup.ResearchgroupMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 课题组 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class ResearchgroupServiceImpl implements ResearchgroupService {

    @Resource
    private ResearchgroupMapper researchgroupMapper;

    @Override
    public Long createResearchgroup(ResearchgroupCreateReqVO createReqVO) {
        // 插入
        ResearchgroupDO researchgroup = ResearchgroupConvert.INSTANCE.convert(createReqVO);
        researchgroupMapper.insert(researchgroup);
        // 返回
        return researchgroup.getId();
    }

    @Override
    public void updateResearchgroup(ResearchgroupUpdateReqVO updateReqVO) {
        // 校验存在
        validateResearchgroupExists(updateReqVO.getId());
        // 更新
        ResearchgroupDO updateObj = ResearchgroupConvert.INSTANCE.convert(updateReqVO);
        researchgroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteResearchgroup(Long id) {
        // 校验存在
        validateResearchgroupExists(id);
        // 删除
        researchgroupMapper.deleteById(id);
    }

    private void validateResearchgroupExists(Long id) {
        if (researchgroupMapper.selectById(id) == null) {
            throw exception(RESEARCHGROUP_NOT_EXISTS);
        }
    }

    @Override
    public ResearchgroupDO getResearchgroup(Long id) {
        return researchgroupMapper.selectById(id);
    }

    @Override
    public List<ResearchgroupDO> getResearchgroupList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return researchgroupMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ResearchgroupDO> getResearchgroupPage(ResearchgroupPageReqVO pageReqVO) {
        return researchgroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResearchgroupDO> getResearchgroupList(ResearchgroupExportReqVO exportReqVO) {
        return researchgroupMapper.selectList(exportReqVO);
    }

}
